package com.networkmanagerj.view.mensagem
{
	import com.networkmanagerj.message.command.ReturnCommand;
	import com.networkmanagerj.message.command.UpdateDate;
	import com.networkmanagerj.message.request.RequestAdslInfo;
	import com.networkmanagerj.message.request.RequestArpTable;
	import com.networkmanagerj.message.response.ResponseAdslInfo;
	import com.networkmanagerj.message.response.ResponseArpTable;
	import com.networkmanagerj.view.equipamentoStatus.EquipamentoStatusGridView;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	import mx.messaging.messages.ErrorMessage;
	import mx.messaging.messages.RemotingMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MessageScript extends MessageView
	{
		private var loadUsuarios:Boolean = true;
		private var loadGrupo:Boolean = true;
		public function MessageScript()
		{
			super();
			remoteObject.getOperation("sendCommand").addEventListener(ResultEvent.RESULT,operationSucess);
			remoteObject.getOperation("sendCommand").addEventListener(FaultEvent.FAULT,operationFailed);
		}
		
		override protected function onCreateComplete(event:FlexEvent):void{
			super.onCreateComplete(event);
			btComUpdateDate.addEventListener(MouseEvent.CLICK,btCommandOnClick);
			btReqAdslInfo.addEventListener(MouseEvent.CLICK,btCommandOnClick);
			btReqArpTable.addEventListener(MouseEvent.CLICK,btCommandOnClick);
		}
		
		private function btCommandOnClick(event:MouseEvent):void{
			currentState = "inicial";
			if(event.currentTarget == btComUpdateDate){
				sendCommandUpdateDate();
			}else if(event.currentTarget == btReqArpTable){
				sendRequestArpTable();
			}else if(event.currentTarget == btReqAdslInfo){
				sendRequestAdslinfo();
			}
		}
		
		private function sendCommandUpdateDate():void{
			remoteObject.sendCommand(new UpdateDate());
			txtAreaMensagem.text = "Processando Comando Update Date...";
		}
		
		private function sendRequestArpTable():void{
			remoteObject.sendCommand(new RequestArpTable());
			txtAreaMensagem.text = "Processando Requisição da ARP TABLE...";
		}
		
		private function sendRequestAdslinfo():void{
			remoteObject.sendCommand(new RequestAdslInfo());
			txtAreaMensagem.text = "Processando Requisição de ADSL INFO...";
		}
		
		override protected function loadDataProvider():void{
			dataProvider =null;
		}
		
		override protected function getAllSucess(event:ResultEvent):void{
			
		}
		
		override protected function operationSucess(event:ResultEvent):void{
			if(RemotingMessage(event.token.message).operation=="sendCommand"){
				if(event.result as ReturnCommand){
					var retCommand:ReturnCommand = event.result as ReturnCommand;
					txtAreaMensagem.text = "\n\nRetorno de comando:\n"+retCommand.getDescriptionType()+
						"\n"+retCommand.message;
				}else if(event.result as ResponseArpTable){
					currentState = "respArpTable";
					responseArpTable = event.result as ResponseArpTable;
				}else if(event.result as ResponseAdslInfo){
					var response:ResponseAdslInfo= event.result as ResponseAdslInfo;
					txtAreaMensagem.text = "\n\nResposta da requisição:\n"+response.mensagem;
				}
			}
		}
		
		override protected function operationFailed(event:FaultEvent):void{
			if(RemotingMessage(event.token.message).operation=="sendCommand"){
				var errorMessage:ErrorMessage = event.message as ErrorMessage;				
				Alert.show(errorMessage.rootCause.message,"Erro ao enviar Mensagem!");
			}
		}
	}
}